//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "math\fn_inShape.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

// Receives a point or object and a shape
// Shape struct: [_center,_size,_angle,_rectangle] 
private ["_shape","_input","_center","_half_width","_half_height","_angle","_rectangle","_inshape","_area"];
DEFAULT_PARAM(_input,0,[]);
DEFAULT_PARAM(_shape,1,[]);

_inshape = false;

_center = [0,0,0];
_angle = 0;
_half_width = 0;
_half_height = 0;
_rectangle = false;

if (IS_OBJECT(_input) && {!isNull _input}) then {
	_input = position _input;
};

if ((!isnil "_shape") && {IS_ARRAY(_shape)}) then {
	_center = GETVAL(_shape,0,ARR_3(0,0,0));
	_half_width = GETVAL(_shape,1,0);
	_half_height = GETVAL(_shape,2,0);
	_angle = GETVAL(_shape,3,0);
	_rectangle = GETVAL(_shape,4,false);
};

if (IS_ARRAY(_input) && {count _input >= 2}) then {
	if (_rectangle) then {
		TRACE("Rectangle Check");
		// RECTANGLE CHECK
		private ["_dist","_dir","_corrected_angle","_cx","_cy"];
		_dist = [_input,_center] call tmf_fnc_distTo;
		_dir = (([_center, _input] call tmf_fnc_dirTo) - 90);
		_corrected_angle = _dir - _angle;
		_cx = abs(_dist * (cos _corrected_angle));
		_cy = abs(_dist * (sin _corrected_angle));
		TRACE_1("Rectangle Values",ARR_3(_dist,_dir,_corrected_angle));
		_inshape = ((_cx <= (_half_width)) && (_cy <= (_half_height)));
	} else {
		if (_half_width == _half_height) then {
			TRACE("Circle Check");
			// CIRCLE CHECK
			private ["_dist"];
			_dist = [_input,_center] call tmf_fnc_distTo;
			TRACE_1("Circle Values",_dist);
			_inshape = (_dist <= _half_width);
		} else {
			TRACE("ELLIPSE Check");
			// ELLIPSE CHECK
			private ["_px","_py","_rx","_ry","_dist","_dir","_corrected_angle","_cx","_cy"];
			_px = (_input select 0) - (_center select 0);
			_py = (_input select 1) - (_center select 1);
			if !(_angle in [0,180]) then {
				if (_angle in [90,270]) then {
					_py = (_input select 0) - (_center select 0);
					_px = (_input select 1) - (_center select 1);
				} else {
					_px = (((_input select 0) - (_center select 0)) * cos _angle) - (((_input select 1) - (_center select 1)) * sin _angle);
					_py = (((_input select 0) - (_center select 0)) * sin _angle) + (((_input select 1) - (_center select 1)) * cos _angle);
				};
			};
			_rx = _half_width;
			_ry = _half_height;
			_cx = (_px * _px) / (_rx * _rx);
			_cy = (_py * _py) / (_ry * _ry);
			TRACE_1("Ellipse Values",ARR_6(_px,_py,_rx_ry,_cx,_cy));
			_inshape = ((_cx + _cy) <= 1);
		};
	};
};

TRACE_1("End",_inshape);
TMFPROFILERSTOP;
_inshape;